<?php

class TeamSeason
{
    const TEAM_EURO_2012        = 'list_team_euro_2012';
    const TEAM_EURO_2012_EXP    = 'list_team_euro_2012_exp';

    const TEAM_SEASON_RANKING = 'bxh_detail';
    /**
     * Define key cache for news team
     */
    const TEAM_DETAIL = 'team_detail';
    /**
     * TeamSeason's instance
     * @var TeamSeason
     */
    protected static $instance;

    protected function __construct()
    {
        
    }

    /**
     * Get TeamSeason's instance
     * @return TeamSeason 
     */
    public static function getInstance()
    {
        if (!isset(self::$instance) || !self::$instance instanceof self)
            self::$instance = new self;
        return self::$instance;
    }

    /**
     * Get full table ranking by season and league
     * @param int $seasonId
     * @param int $leagueId 
     * @return array
     */
    public function getListTableRanking($seasonId, $leagueId)
    {
        $memcacheInstance = Fpt_Global::getMemcacheInstance($seasonId + $leagueId);
        $keyCache = Fpt_Global::makeCacheKey(self::TEAM_SEASON_RANKING, array($leagueId, $seasonId));

        // Read cache
        $tableRanking = $memcacheInstance->read($keyCache);

        // If miss cache
        if ($tableRanking === FALSE)
        {
            try
            {
                $config = Fpt_Global::getApplicationIni();
                // Get db teamseason instance
                $teamseasonObj = Fpt_Business_TeamSeason::factory($config['database']['default']);

                // Get data from db
                $tableRanking = $teamseasonObj->getListTableRanking($seasonId, $leagueId);
                
                // Write to cache
                Fpt_Global::writeMemcache($seasonId + $leagueId, $keyCache, $tableRanking);
            }
            catch (Zend_Exception $ex)
            {
                Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            }
        }
        return $tableRanking;
    }
    /**
     * Get Team Detail
     * @param int $teameID
     * @return array
     * @author  : trint
     */
    public function getDetailTeam($teamID)
    {
        $memcacheInstance = Fpt_Global::getMemcacheInstance($teamID);
        $keyCache = Fpt_Global::makeCacheKey(self::TEAM_DETAIL, $teamID);

//        Fpt_Global::deleteMemcache($teamID, $keyCache);
        $arrTeamDetail = $memcacheInstance->read($keyCache);

        // Not in cache?
        if ($arrTeamDetail === FALSE)
        {
            try
            {
                $config = Fpt_Global::getApplicationIni();
                // Get mysql new instance
                $newsMysql = Fpt_Business_TeamSeason::factory($config['database']['default']);
                $arrTeamDetail = $newsMysql->getDetailTeam($teamID);
                if (!empty($arrTeamDetail))
                {
                    // Store into cache
                    Fpt_Global::writeMemcache($teamID, $keyCache, $arrTeamDetail);
                }
            }
            catch (Zend_Exception $ex)
            {
                Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            }
        }

        // Return result
        return $arrTeamDetail;
    }

    /**
     * edit Team Detail
     * @param int $teamID
     * @return array
     * @author  PhuongTN
     */
    public function editTeam($teamID)
    {
        try
        {
            $response = false;
            
            $keyCache = Fpt_Global::makeCacheKey(self::TEAM_DETAIL, $teamID);
            $response = Fpt_Global::deleteMemcache($teamID, $keyCache);
            //precaching
            $this->getDetailTeam($teamID);
        }
        catch (Zend_Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
        }
        // Return result
        return $response;
    }

    /**
     * Get ranking euro 2012
     */
    
    public function getRankingEuro2012($seasonId=57, $leagueId=8, $list=null)
    {
        try
        {
            global $arrGroupTable;
            $arrData    = array();

            //Get list team ranking euro2012
            if ( empty($list) || !is_array($list) )
            {
                $arrRanking = $this->getListTableRanking($seasonId, $leagueId);
            }
            else
            {
                $arrRanking = $list;
            }//end if
            if ( !empty($arrRanking) && is_array($arrRanking) )
            {
                $tmp    = array();
                foreach ( $arrRanking as $row )
                {
                    $tmp[$row['Table']]['Team'][]   = $this->mapDataRankingEuro2012($row);
                }//end foreach
                foreach ( $arrGroupTable as $code=>$groupName )
                {
                    $arrData[$code]['Name'] = $groupName;
                    $arrData[$code]['Team'] = $tmp[$code]['Team'];
                }//end foreach
            }//end if
        } catch (Exception $ex) {
            //log error
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
        }//end try catch
        
        //Return
        return $arrData;
    }

    /**
     * Map data ranking euro 2012
     */
     
    public function mapDataRankingEuro2012($row)
    {
        //Set return
        $return = $row;

        //Set param
        foreach ( $row as $key=>$val )
        {
            $return[strtolower($key)]   = $row[$key];
        }//end foreach
        $return['logo']     = '';
        $return['team_id']  = $row['TeamID'];
        
        //Return
        return $return;
    }
   
}